package cn.jobstart.spider.parsing;

import cn.jobstart.common.*;
import cn.jobstart.data.DataUtil;
import cn.jobstart.enums.ResumeBasicSexFlagEnum;
import cn.jobstart.enums.TalentSubscriptionTaskChannelEnum;
import cn.jobstart.es.vo.ResumeEsMatching;
import cn.jobstart.spider.model.*;
import cn.jobstart.vo.*;
import com.alibaba.fastjson.JSONArray;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author sunke
 * @version 1.0
 * @Description 51job 人才解析
 * @date 2023-11-13 15:22
 */
public class TalentJob51Parsing implements IParsing<TalentJob51> {


    private DataUtil dataUtil;
    private String talentSubscriptionTaskId;

    public TalentJob51Parsing(String talentSubscriptionTaskId) {
        this.dataUtil = (DataUtil) SpringUtils.getApplicationContext().getBean("dataUtil");
        this.talentSubscriptionTaskId = talentSubscriptionTaskId;
    }

    public DataUtil getDataUtil() {
        return dataUtil;
    }

    public TalentJob51Parsing setDataUtil(DataUtil dataUtil) {
        this.dataUtil = dataUtil;
        return this;
    }

    public String getTalentSubscriptionTaskId() {
        return talentSubscriptionTaskId;
    }

    public TalentJob51Parsing setTalentSubscriptionTaskId(String talentSubscriptionTaskId) {
        this.talentSubscriptionTaskId = talentSubscriptionTaskId;
        return this;
    }

    /**
     * 添加工作经历
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addWorkExperience(TalentJob51 talentJob51Item, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentJob51Work> workList = talentJob51Item.getWork();
        if (StringUtil.isNullOrEmpty(workList)) {
            return;
        }
        List<ResumeWorkExperienceMiniDomainVO> workExperienceList = SkList.getInstance();

        workList.stream().forEach(item -> {
            try {
                ResumeWorkExperienceMiniDomainVO itemVo = new ResumeWorkExperienceMiniDomainVO();

                itemVo.setUnitName(StringUtil.delHTMLTag(item.getCompany()));
                itemVo.setJobName(StringUtil.delHTMLTag(item.getPosition()));
                itemVo.setContent(item.getWorkFuncStr());
                if (StringUtil.isNullOrEmpty(itemVo.getContent())) {
                    itemVo.setContent(item.getWorkdescribe());
                }


                itemVo.setStartTime(formatDate(item.getStartTime()));
                itemVo.setEndTime(formatDate(item.getEndTime()));


                workExperienceList.add(itemVo);
            } catch (Exception e) {

            }


        });


        if (StringUtil.isNotObjEmpty(workExperienceList)) {

            vo.setWorkExperience(JSONArray.toJSONString(workExperienceList));
            vo.setWorkExperienceList(workExperienceList);


        }


    }

    private Date formatDate(String time) {

        return formatDate(time, "yyyy.MM");

    }

    private Date formatDate(String time, String format) {

        if (StringUtil.isNullOrEmpty(time)) {
            return null;
        }
        if (StringUtil.isNullOrEmpty(format)) {
            format = "yyyy.MM";
        }
        if ("至今".equalsIgnoreCase(time)) {
            return new Date();
        }
        try {
            return YXDate.getString2Date(time, format);
        } catch (Exception e) {

        }
        return null;

    }

    /**
     * 添加项目经历
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addProjectExperience(TalentJob51 talentJob51Item, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentJob51Project> projectList = talentJob51Item.getProject();
        if (StringUtil.isNullOrEmpty(projectList)) {
            return;
        }
        List<ResumeProjectExperienceMiniDomainVO> projectExperienceList = SkList.getInstance();

        projectList.stream().forEach(item -> {
            try {
                ResumeProjectExperienceMiniDomainVO itemVo = new ResumeProjectExperienceMiniDomainVO();
                itemVo.setContent(item.getDescribe());
                itemVo.setTitle(StringUtil.delHTMLTag(item.getProjectName()) + "-" + StringUtil.delHTMLTag(item.getCompany()));

                itemVo.setStartTime(formatDate(item.getTimeFrom()));
                itemVo.setEndTime(formatDate(item.getTimeTo()));


                projectExperienceList.add(itemVo);
            } catch (Exception e) {

            }


        });


        if (StringUtil.isNotObjEmpty(projectExperienceList)) {

            vo.setProjectExperience(JSONArray.toJSONString(projectExperienceList));
            vo.setProjectExperienceList(projectExperienceList);


        }


    }

    /**
     * 添加求职意向
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addEmploymentIntention(TalentJob51 talentJob51Item, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentJob51ResumeExpectInfo> resumeExpectInfoList = talentJob51Item.getResumeExpectInfoList();
        if (StringUtil.isNullOrEmpty(resumeExpectInfoList)) {
            return;
        }
        List<ResumeEmploymentIntentionMiniDomainVO> employmentIntentionList = SkList.getInstance();

        resumeExpectInfoList.stream().forEach(item -> {
            try {
                ResumeEmploymentIntentionMiniDomainVO itemVo = new ResumeEmploymentIntentionMiniDomainVO();
                itemVo.setJob(StringUtil.delHTMLTag(item.getExpectWorkFuncStr()));
                itemVo.setSalary(item.getExpectSalary());
                itemVo.setCity(item.getExpectJobAreaStr());

                employmentIntentionList.add(itemVo);
            } catch (Exception e) {

            }


        });


        if (StringUtil.isNotObjEmpty(employmentIntentionList)) {

            vo.setEmploymentIntention(JSONArray.toJSONString(employmentIntentionList));
            vo.setEmploymentIntentionList(employmentIntentionList);


        }


    }

    /**
     * 添加教育经历
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addEducationalExperience(TalentJob51 talentJob51Item, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentJob51Education> educationList = talentJob51Item.getEducation();
        if (StringUtil.isNullOrEmpty(educationList)) {
            return;
        }
        List<ResumeEducationalExperienceMiniDomainVO> educationalExperienceList = SkList.getInstance();

        educationList.stream().forEach(item -> {

            ResumeEducationalExperienceMiniDomainVO itemVo = new ResumeEducationalExperienceMiniDomainVO();

            itemVo.setTitle(StringUtil.delHTMLTag(item.getSchool()));
            itemVo.setUnitName(StringUtil.delHTMLTag(item.getMajorStr()));
            itemVo.setUnitType(item.getDegreeStr());
            itemVo.setSchoolLabelFlag(dataUtil.getSchoolLabelBySchoolName(item.getSchool()));

            itemVo.setStartTime(formatDate(item.getStartTime()));
            itemVo.setEndTime(formatDate(item.getEndTime()));


            educationalExperienceList.add(itemVo);


        });


        if (StringUtil.isNotObjEmpty(educationalExperienceList)) {

            vo.setEducationalExperience(JSONArray.toJSONString(educationalExperienceList));

            vo.setEducationalExperienceList(educationalExperienceList);


        }


    }

    /**
     * 添加证书
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addCertificate(TalentJob51 talentJob51Item, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentJob51Certification> certificationList = talentJob51Item.getCertification();
        if (StringUtil.isNullOrEmpty(certificationList)) {
            return;
        }
        List<ResumeCertificateMiniDomainVO> certificateList = SkList.getInstance();

        certificationList.stream().forEach(item -> {

            try {
                ResumeCertificateMiniDomainVO itemVo = new ResumeCertificateMiniDomainVO();
                itemVo.setUnitName(StringUtil.delHTMLTag(item.getCert()));
                certificateList.add(itemVo);


            } catch (Exception e) {

            }


        });


        if (StringUtil.isNotObjEmpty(certificateList)) {

            vo.setCertificate(JSONArray.toJSONString(certificateList));
            vo.setCertificateList(certificateList);


        }


    }

    /**
     * 添加能力
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addCapabilityLabel(TalentJob51 talentJob51Item, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        List<TalentJob51Itskill> itskillList = talentJob51Item.getItskill();
        if (StringUtil.isNullOrEmpty(itskillList)) {
            return;
        }

        List<ResumeCapabilityLabelMiniDomainVO> capabilityLabelList = SkList.getInstance();

        itskillList.stream().forEach(item -> {

            try {
                ResumeCapabilityLabelMiniDomainVO itemVo = new ResumeCapabilityLabelMiniDomainVO();
                itemVo.setText(StringUtil.delHTMLTag(item.getItType()));
                itemVo.setContent(item.getAbility());

                capabilityLabelList.add(itemVo);

            } catch (Exception e) {


            }


        });
        if (StringUtil.isNotObjEmpty(capabilityLabelList)) {
            vo.setCapabilityLabel(JSONArray.toJSONString(capabilityLabelList));
            vo.setCapabilityLabelList(capabilityLabelList);
        }


    }

    /**
     * 添加基础信息
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addBasic(TalentJob51 talentJob51Item, ResumeEsMatching vo) {

        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }

        TalentJob51BaseInfo baseInfo = talentJob51Item.getBaseInfo();

        if (StringUtil.isNullOrEmpty(baseInfo)) {
            return;
        }

        //将年龄传过去 ，
        vo.setThirdAge(NumberUtil.parseInt(baseInfo.getAge(),-1));


        vo.setThirdAccountId(baseInfo.getAccountId());

        TalentJob51ResumeExpectInfo expectInfo = talentJob51Item.getResumeExpectInfo();
        if (StringUtil.isNotObjEmpty(expectInfo)) {
            vo.setExceptCity(expectInfo.getExpectJobAreaStr());
            vo.setCategory(expectInfo.getExpectWorkFuncStr());
            vo.setExceptWork(expectInfo.getExpectWorkFuncStr());
            try {
                vo.setExpectedSalary(new BigDecimal(expectInfo.getExpectMaxSalary()));
            } catch (Exception e) {

            }
        }


        vo.setSource(TalentSubscriptionTaskChannelEnum.JOB51.getType());
        vo.setTitle(baseInfo.getUserName() + "的简历");
        vo.setMarriage("保密");
        vo.setMarriageFlag(0);
        vo.setBirthPlace(baseInfo.getAreaStr());
        vo.setTruename(baseInfo.getUserName());
        vo.setSex(baseInfo.getSexStr());
        int sexFlag = 0;
        if ("男".equalsIgnoreCase(baseInfo.getSexStr())) {
            sexFlag = ResumeBasicSexFlagEnum.MALE.getType().intValue();
        } else if ("女".equalsIgnoreCase(baseInfo.getSexStr())) {
            sexFlag = ResumeBasicSexFlagEnum.FEMALE.getType().intValue();
        }
        vo.setSexFlag(sexFlag);
        int age = NumberUtil.parseInt(baseInfo.getAge());
        if (age > 0) {
            vo.setBirthday(YXDate.addYear(new Date(), -age));
        }
        vo.setLivePlace(baseInfo.getAreaStr());
        vo.setAge(age);

        vo.setResumeCreateTime(new Date());
        vo.setResumeModifyTime(new Date());
        vo.setJobYear(baseInfo.getWorkYearStr());
        vo.setEducation(baseInfo.getTopDegreeStr());
        vo.setSchool(baseInfo.getTopSchoolName());
        vo.setSpeciality(baseInfo.getTopMajorStr());
        vo.setSummary(baseInfo.getSelfIntro());


    }


    /**
     * 添加在校经历
     *
     * @param talentJob51Item
     * @param vo
     */
    private void addSchoolJob(TalentJob51 talentJob51Item, ResumeEsMatching vo) {
        if (StringUtil.isNullOrEmpty(talentJob51Item) || StringUtil.isNullOrEmpty(vo)) {
            return;
        }
        List<TalentJob51SchoolJob> schoolJobList = talentJob51Item.getSchooljob();
        if (StringUtil.isNullOrEmpty(schoolJobList)) {
            return;
        }

        List<ResumeAssociationActivityMiniDomainVO> associationActivityList = SkList.getInstance();

        schoolJobList.stream().forEach(item -> {

            try {
                ResumeAssociationActivityMiniDomainVO itemVo = new ResumeAssociationActivityMiniDomainVO();
                itemVo.setTitle(StringUtil.delHTMLTag(item.getSchoolJobName()));
                itemVo.setUnitName(item.getSchoolJobDesc());

                itemVo.setStartTime(formatDate(item.getTimeFrom()));
                itemVo.setEndTime(formatDate(item.getTimeTo()));


                associationActivityList.add(itemVo);
            } catch (Exception e) {

            }

        });

        if (StringUtil.isNotObjEmpty(associationActivityList)) {
            vo.setAssociationActivity(JSONArray.toJSONString(associationActivityList));
            vo.setAssociationActivityList(associationActivityList);
        }


    }

    /**
     * 解析爬虫过来的数据
     *
     * @param talentJob51Item
     * @return
     */
    @Override
    public ResumeEsMatching parsing(TalentJob51 talentJob51Item) {
        if (StringUtil.isNullOrEmpty(talentJob51Item)) {
            return null;
        }

        ResumeEsMatching vo = new ResumeEsMatching();
        vo.setTalentSubscriptionTaskId(talentSubscriptionTaskId);
        String resumeId = IdGen.uuid();
        vo.setId(resumeId);

        //基本信息
        addBasic(talentJob51Item, vo);
        //在校经历
        addSchoolJob(talentJob51Item, vo);
        //能力
        addCapabilityLabel(talentJob51Item, vo);
        //证书
        addCertificate(talentJob51Item, vo);
        //添加教育
        addEducationalExperience(talentJob51Item, vo);

        //添加求职意向
        addEmploymentIntention(talentJob51Item, vo);
        //添加项目经历
        addProjectExperience(talentJob51Item, vo);
        //添加工作经历
        addWorkExperience(talentJob51Item, vo);

        return vo;
    }
}
